!> author: 左志华
!> date: 2022-07-20
!>
!> Pre/Post-process the data <br>
!> 前后处理
module sph

    use seakeeping_filesystem, only: operator(.join.)
    use sph_command_line, only: pre_settings_t, post_settings_t
    use sph_region_type, only: region_t
    use sph_save_h5part, only: save_h5part
    use sph_load_h5part, only: load_h5part
    use sph_load_lua, only: load_lua_script
    use sph_terminal, only: blue

contains

    !> Pre process the data <br>
    !> 前处理数据
    subroutine cmd_pre(cmd_settings, region)
        type(pre_settings_t), intent(in) :: cmd_settings  !! 前处理设置
        type(region_t), intent(out) :: region  !! 粒子域
        select case (cmd_settings%type)
        case ("lua", "LUA")
            write (*, '(a)') blue('Pre-processing: Load lua script ...')
            call load_lua_script(cmd_settings%working_dir.join.'pif.lua', region)
        case default
            error stop "Unsupported pre-processing type: "//cmd_settings%type
        end select

        write (*, '(a)') blue('Pre-processing: Save h5part file ...')
        call save_h5part(file=cmd_settings%working_dir.join.'pif.h5part', &
                         nml=cmd_settings%working_dir.join.'pif.nml', &
                         region=region, &
                         skip=cmd_settings%skip)
        call region%info()
    end subroutine cmd_pre

    !> Post process for the results of the simulation <br>
    !> 对结果的后期处理
    subroutine cmd_post(cmd_settings, region)
        use seakeeping_filesystem, only: is_exist
        use sph_terminal, only: green
        type(post_settings_t), intent(in) :: cmd_settings   !! Post process settings <br>
                                                            !! 后期处理设置
        type(region_t), intent(out) :: region               !! Region <br>
                                                            !! 计算域

        select case (cmd_settings%type)
        case default
            error stop "Unsupported post-processing type: "//cmd_settings%type
        end select

    end subroutine cmd_post

end module sph
